package com.wangyadong.hobby.schedule.jobtask.dsa;

import java.util.Stack;

public class ValidateStackSequences {

    public boolean solution(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int index = 0;
        for (int num : pushed) {
            stack.add(num);
            while (!stack.isEmpty() && stack.peek() == popped[index]) {
                stack.pop();
                index++;
            }
        }

        return stack.isEmpty();
    }

    public static void main(String[] args) {
        ValidateStackSequences stackSequences = new ValidateStackSequences();
        int[] push = {1, 2, 3, 4, 5};
//        int[] pop = {4, 5, 3, 2, 1};
        int[] pop = {5, 4, 3, 2, 1};
//        int[] pop = {2, 3, 4, 5, 1};
        boolean solution = stackSequences.solution(push, pop);
        System.out.println(solution);
    }
}
